package com.dlsy.ygj.dao.vindicateData.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.util.CollectionUtils;

import com.dlsy.ygj.dao.vindicateData.IDepartmentDAO;
import com.dlsy.ygj.entity.Department;

import net.taobaoge.common.framework.dao.impl.BaseDAOImpl;

public class DepartmentDAO extends BaseDAOImpl<Department> implements IDepartmentDAO {

	@SuppressWarnings("unchecked")
	public List<Department> findDepartmentByProperty(String value,Integer stroreId) {
		Criteria criteria=getSession().createCriteria(Department.class);
		criteria.add(Restrictions.and(Restrictions.or(Restrictions.like("departmentNo", "%"+value+"%"), Restrictions.like("departmentName", "%"+value+"%")),Restrictions.eq("storefrontId", stroreId)));
		List<Department> departments=criteria.list();
		if(departments !=null && departments.size()>0){
			return departments;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Department> checkExist(final Integer departmentId,final String value,final Integer stroreId){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			
			public List<Department> doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria criteria=getSession().createCriteria(Department.class);
				criteria.add(Restrictions.and(Restrictions.and(Restrictions.or(Restrictions.eq("departmentNo", value), Restrictions.eq("departmentName",value)),Restrictions.not(Restrictions.eq("departmentId", departmentId))),Restrictions.eq("storefrontId", stroreId)));
				List<Department> departments=criteria.list();
				if(departments !=null && departments.size()>0){
					return departments;
				}
				return null;
			}
		});
	}
	public List<Department> findAll(Integer stroreId) {
		try {
			List<Department> departments = findByProperty(Department.class,
					"storefrontId", stroreId);
			if(!CollectionUtils.isEmpty(departments)){
				return departments;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Department> checkExist(final String value,
			final Integer stroreId) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public List<Department> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = getSession().createCriteria(
						Department.class);
				criteria.add(Restrictions.and(Restrictions.or(Restrictions.eq(
						"departmentNo", value), Restrictions.eq(
						"departmentName", value)), Restrictions.eq(
						"storefrontId", stroreId)));
				List<Department> departments = criteria.list();
				if (departments != null && departments.size() > 0) {
					return departments;
				}
				return null;
			}
		});
	}

}
